#!/bin/sh
#---------------------------------------------------------------------------------
# backuphdr.sh
# 
# This shell script is invoked to perform a backup of the HSC Harddisk for Disaster
# Recovery purposes. All of the files that have changed since the code was installed on
# the hard disk will be placed into a compressed tar file on the removable media. 
# 
# Usage: backuphdr (no parameters)
#
# Return Codes:
# 1 - Error locating required backup directory
# 2 - Not used
# 3 - Error unmounting the media
# 4 - Media is write-protected
# 5 - Error mounting the media
# 6 - Other errors ('tar' command error)

#---------------------------------------------------------------------------------
# directory and filename which records the backup actions.
#---------------------------------------------------------------------------------
LOGDIR=/var/hsc/log
LOG=$LOGDIR/backuphdr.log
   
LOG_ERROR_LOG=/tmp/backuphdr.log

#---------------------------------------------------------------------------------
# mount point: the mount point for the media to which the backup is to be stored
# ??? - this must be changed to mount point for DVD RAM
# mountpoint for DVD-RAM now defined (yes, it really is to /mnt/cdrom. 'fstab'
# entry wil automount correct physical device) 4/25/01 - SLF
#---------------------------------------------------------------------------------
MOUNTPOINT=/mnt/cdrom

#---------------------------------------------------------------------------------
# the filename of the backup archive
#---------------------------------------------------------------------------------
ARCHIVE=backuphdr.tgz

#---------------------------------------------------------------------------------
# a file whose date and time reflect when the build of the HSC was performed
#---------------------------------------------------------------------------------
MARKER=/opt/hsc/com/ibm/hsc/websm/launch/hscmgt/hscbuild.dat


# Check if the directory for the log file exists.
if [ ! -d $LOGDIR ]; then
	echo "=================================================================" > $LOG_ERROR_LOG
	echo -e "Backup task log for `date`." >> $LOG_ERROR_LOG
	echo "Backup task log directory, <$LOGDIR>, does not exist. Program exiting" >> $LOG_ERROR_LOG
	exit 1
fi

# Start log to record backup actions.
echo -e "Backup log for `date`.\n" > $LOG


# Check to see if media is already mounted - if so then unmount it
if grep "$MOUNTPOINT" /etc/mtab; then # Media is already mounted
	if ! umount $MOUNTPOINT >> $LOG 2>&1; then
		echo "Couldn't unmount the media, $MOUNTPOINT." >> $LOG
		exit 3
	fi
fi


#
# Generate a list of *most* (not *all*) of the files that have changed since the
# code was installed on the hard disk (as indicated by the marker file).
#
# Set the file to output this list of files to. This avoids the problem of overflowing
# the 'tar' input buffer if too many files are present.
#
# Make sure to catch the sybolic link'd files too and also set up the filter
# to catch ".*" files as well
#
# Currently, there are still issues with saving directories only.  Problem is
# if we identify a directory to backup, the 'tar' command seems to decend into
# that directory and save all files contained in that directory whether or not
# they meet the date filtering criteria. See the comments in the 'restore' script
# for further details regarding users' home directories.
#
# Some files still get archived that shouldn't (some PID files, for example),
# but the system seems tolerant of the recovery of these files.
#
FILELIST=/tmp/backup.list
`find / -name '*' -xtype f ! -path '/var/lock/subsys/*' ! -path '/extra-swap' ! -path '/tmp/archive.log' ! -path '/tmp/backup.list' ! -path '/proc/*' ! -type b ! -type c ! -type p ! -type s -newer $MARKER -print > $FILELIST`
`find / -name '.*' -xtype f ! -path '/var/lock/subsys/*' ! -path '/proc/*' ! -type b ! -type c ! -type p ! -type s -newer $MARKER -print >> $FILELIST`

#                      
# Now we need to query all the existing RPM packages on the system in case PTF
# updates added any new RPMs that contain files older than the marker file
#
# Just in case we have NLS troubles reading system information...
#
LANG=en_US
export LANG

# Mount the media and check for write-protect
if mount -v $MOUNTPOINT > mount_output 2>&1; then
	echo "Mounted the media." >> $LOG
	if grep "write-protected" mount_output; then
		echo "The media is write-protected." >> $LOG
		umount $MOUNTPOINT
		exit 4
	fi
else
	echo "Couldn't mount the media, $MOUNTPOINT." >> $LOG
	exit 5
fi

# Form the name of the archive file by concatenating the directory and filename.
archive="$MOUNTPOINT/$ARCHIVE"

#
# Begin the archive process.
# ToDo: Need to check for diskspace problems or other errors before/after tar cmd runs
# Also attempt to verify the archive, but there appear to be issues with verifying
# a compressed file. Odd...
# 
echo "Creating new archive..." >> $LOG
tar --create --gzip --verbose --absolute-names --file=$archive --files-from=$FILELIST >> $LOG 2>&1
TAR_RC=$?

# Although useful for debug purposes, cleanup the list of files to archive
rm -f $FILELIST

# check for 'tar' errors.  A '2' appear to be returned if we attempt to 'tar' a
# "corrupt" file, example: broken symbolic link
if [ $TAR_RC -ne 0 || $TAR_RC -ne 2 ]; then
	echo "tar command failure. Return code is $?." >> $LOG
	umount $MOUNTPOINT
	exit 6
fi

# Unmount the removable media
if umount -v $MOUNTPOINT >> $LOG 2>&1; then
	echo "Backup of critical system data completed successfully on `date`." >> $LOG
	exit 0
else
	echo "Couldn't unmount the media." >> $LOG
	echo "Backup of critical system data completed on `date`." >> $LOG
	exit 3
fi

